//
//  PaperAnswerModel.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/3/10.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperAnswerModel.h"
#import "PaperAnalyModel.h"

@implementation PaperAnswerModel

+ (NSDictionary *)objectClassInArray {
    return @{@"ques_stem"  : [PaperTypeModel class],
             @"option"     : [PaperOptionModel class],
             @"abstract"   : [PaperTypeModel class]};
}

- (void)mj_keyValuesDidFinishConvertingToObject {
    self.index = self.ques_model == 0 || self.ques_model == 2 ? NSNotFound : 0;
    
    // 材料
    if (self.abstract && self.abstract.count > 0) {
        self.showMaterial = YES;
        [self.abstract insertObject:[PaperTypeModel createQuestTestMaterial] atIndex:0];
        self.materialLayout = [YYTextLayout layoutWithTexts:self.abstract];
    }
    // 每道题前面增加【单选题】or【多选题】
    [self.ques_stem insertObject:[PaperTypeModel createQuestKindWithType:self.ques_model andStr:[NSString stringWithFormat:@"%ld/%ld",self.ques_num, self.ques_count]] atIndex:0];
    self.textLayout = [YYTextLayout layoutWithTexts:self.ques_stem];
    
    // option
    self.optionLayouts = [NSMutableArray arrayWithCapacity:self.option.count];
    [self.option enumerateObjectsUsingBlock:^(PaperOptionModel *optionModel, NSUInteger idx, BOOL * _Nonnull stop) {
        PaperAnswerOptionModel *model = [[PaperAnswerOptionModel alloc] initWithPaperModel:optionModel];
        [self.optionLayouts addObject:model];
    }];
}

- (NSString *)answer {
    
    if (self.ques_model == 2 && self.user_content) {
        return self.user_content;
    }
    
    __block NSMutableString *str = [NSMutableString string];
    [self.optionLayouts enumerateObjectsUsingBlock:^(PaperAnswerOptionModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        if (model.type == PaperOptionTypeSelect) {
            if (str.length != 0) {
                [str appendString:@","];
            }
            [str appendString:model.title ? : @""];
        }
    }];
    return str;
}

 
@end
